<link rel="stylesheet" href="../static/css/style.css">
<link rel="stylesheet" href="../static/css/styleoneadd.css">
<link rel="stylesheet" href="../static/plugins/layui/css/layui.css">
<link rel="stylesheet" href="../static/plugins/bootstrap-3.3.7/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="../static/plugins/bootstrap-3.3.7/css/bootstrap.min.css">

<style>
	body{
		background-color: #f2f2f2 !important;
	}
</style>

<!-- 内容主体区域 -->
<div class="layui-fluid-pad" ng-app="paper">
	<div class="layui-col-md12" ng-controller="paperController" id="paperController">
		<div class="layui-card">
			<div class="layui-card-header">问卷管理</div>
			<div class="layui-card-body">
				<div class="layui-row layui-pad15">
					<form class="layui-form" action="" lay-filter="test_form">
						<div class="layui-col-md12 layui-col-lh">
							<div class="layui-row layui-col-space10">
								<div class="layui-col-md2"><input type="text" name="title" id="paperName" placeholder="问卷名称" class="form-control" /></div>
								<div class="layui-col-md2" >
									<select name="city" class="form-control" id="paperType" lay-ignore>
										<option value="">问卷类型</option>
										<option value="{{type.typeName}}" ng-repeat="type in typeList">{{type.typeName}}</option>
										<!--<option value="0">北京</option>
										<option value="1">上海</option>
										<option value="2">广州</option>
										<option value="3">深圳</option>
										<option value="4">杭州</option>-->
									</select>
								</div>
								<div class="layui-col-md3">
									<div class="layui-row">
										<div class="layui-col-md12 layui-order-flexs">
											<input type="text" class="form-control layui-gd-widths" placeholder="时间" id="test1">
											&nbsp;&nbsp;至&nbsp;&nbsp;
											<input type="text" class="form-control layui-gd-widths" placeholder="时间" id="test2">
										</div>
									</div>
								</div>
								<div class="layui-col-md1">
									<button type="button" class="layui-btn  layui-t-h" ng-click="screen()">筛选</button>
								</div>
								<div class="layui-col-md4 layui-text-right">
									<a href="createPaper.html" class="layui-btn layui-t-h creatdx">
										<i class="layui-icon">&#xe63c;</i>
										创建问卷
									</a>
								</div>
							</div>
						</div>
					</form>

					<!--表格-->
					<div class="layui-row">
						<div class="layui-col-md12">
							<table class="layui-data-zs" id="test3" border="1" style="    border: 1px solid #e5e5e5;">
								<thead>
									<tr class="layui-t-h layui-hg-b" style=" background-color: #34C0E3;color: #ffffff;">
										<th>序号</th>
										<th>问卷名称</th>
										<th>问卷类型</th>
										<th>创建时间</th>
										<th>操作</th>
									</tr>
								</thead>
								<tbody>
									<tr ng-repeat="y in paperList" class="layui-hg-b">
										<td><span ng-bind="$index + 1"></span></td>
										<td><span ng-bind="y.name || '--'"></span></td>
										<td><span ng-bind="y.type || '--'"></span></td>
										<td><span ng-bind="(y.createdTime|date:'yyyy-MM-dd') || '--'"></span></td>
										<td>
											<a href="#" ng-click="turnModify(y.id)" class="layui-btn-ty layui-zdy-bj">编辑</a>
											<a href="#" class="layui-btn-ty  layui-dels" ng-click="dltPaper(y.id)">删除</a>
										</td>
									</tr>
								</tbody>
							</table>
							<div ng-show="!paperList || paperList.length == 0" style="margin: 20px 0; text-align: center;">无符合条件数据。</div>
						</div>
						<!--分页-->
						<div class="layui-col-md12 layui-page-top" ng-hide="!paperList || paperList.length == 0">
							<div id="demo7"></div>
						</div>
						<!--分页end-->
					</div>
					<!--表格end-->
				</div>

			</div>
		</div>
	</div>
</div>

<script src="../static/js/jquery.min.js"></script>
<script src="../static/plugins/layui/layui.js"></script>
<script src="../static/plugins/bootstrap-3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../static/js/angular.js"></script>
<script src="../static/js/app/consts/consts.js"></script>
<script>

	var currentPageNumber = 1;
	var currentPageSize = 10;
	var currentPageCount = 0;

	var getAllPaperURL = baseUrl + "/exam/getAllPaper";
	var getAllTypeURL = baseUrl + "/exam/getAllType";
	var dltPaperURL = baseUrl + "/exam/dltPaper";
	var getPartPaperURL = baseUrl + "/exam/getPartPaper";

	var app = angular.module('paper', ['ng']);
	app.controller('paperController', function($scope,$http) {

        function getAllPaper() {
            $http({
                method: 'GET',
                params:{
                    token:"11111",
                    pageNumber: currentPageNumber,
                    pageSize: currentPageSize,
                    sort: "",
                    desc: ""
                },
                url: getAllPaperURL
            }).then(function successCallback(result) {
                if(result.data) {
                    var resultInfo = result.data.data;
                    console.log(resultInfo);
                    updateUnitermPagination(resultInfo);
                    if(resultInfo) {
                        $scope.paperList = resultInfo.data;
                    }
                }
            }, function errorCallback(response) {
                console.log("err");
            });
        }

		//页面加载时执行的函数
		$http({
			method: 'GET',
			params:{
			    token:"11111",
                pageNumber: currentPageNumber,
                pageSize: currentPageSize,
                sort: "",
                desc: ""
			},
			url: getAllPaperURL
		}).then(function successCallback(result) {
            if(result.data) {
                var resultInfo = result.data.data;
                console.log(resultInfo);
                updateUnitermPagination(resultInfo);
                if(resultInfo) {
                    $scope.paperList = resultInfo.data;
                }
            }
		}, function errorCallback(response) {
			console.log("err");
		});
		$http({
			method:"GET",
			url:getAllTypeURL,
			params:{
			    token:"123456"
			}
		}).then(function successCallback(result) {
			$scope.typeList = result.data.data;
			console.log($scope.typeList);
        });

//		layui.use(['form'],function () {
//            var form = layui.form;
//            $(function () {
//				form.render('select','test_form');
//            })
//        });

		//删除按钮
		$scope.dltPaper = function (paperId) {
            layer.open({
                area: '300px;',
                title: '确认',
                content: '确认删除？',
                btn: ['确认', '取消'],
                closeBtn: false,
                resize: false,
                yes: function (index, layero) {
                    $http({
                        method:"POST",
                        url:dltPaperURL,
                        data:{
                            token:"000000",
                            paperId:paperId
                        },
                        headers:{'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
                        transformRequest: function(obj) {
                            var str = [];
                            for(var p in obj){
                                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                            }
                            return str.join("&");
                        }
                    }).then(function successCallback(result) {
                        console.log(result.data.state === 1);
                        if(result.data.state === 1) {
                            $scope.screen(currentPageNumber);
                            layer.msg("删除成功");
                        }
                    });
                }
            });
        }

		//编辑按钮
		$scope.turnModify = function (paperId) {
			console.log(paperId);
            var url = "modifyPaper.html?paperId=" + paperId;
            window.location.href = url;
        }


		//筛选按钮
		$scope.screen = function (goPageNumber) {
            if(!goPageNumber) {
                currentPageNumber = 1;
            } else {
                currentPageNumber = goPageNumber;
            }
            var data = {
                token:"22222",
                paperName:$("#paperName").val()?($("#paperName").val()):"",
                paperType:$("#paperType").val()?($("#paperType").val()):"",
                startTime:$('#test1').val()?($('#test1').val()):"",
                endTime:$('#test2').val()?($('#test2').val()):"",
                pageNumber: currentPageNumber,
                pageSize: currentPageSize,
                sort: "",
                desc: ""
            };
            $http({
                method:"GET",
                params:data,
                url:getPartPaperURL
			}).then(function successCallback(result) {
                if(result.data) {
                    var resultInfo = result.data.data;
                    updateUnitermPagination(resultInfo);
                    if(resultInfo) {
                        $scope.paperList = resultInfo.data;
                    }
//                    if(result.data.msg) {
//                        layer.msg(result.data.msg)
//                    }
                }
            },function errorCallback(result) {
				console.log(result.data.message);
            });
        }
	});

    // 获取angular scope
    function getAngularScope() {
        return angular.element(paperController).scope();
    }

	function updateUnitermPagination(resultInfo) {
		currentPageCount = resultInfo.total || 0;
		currentPageNumber = resultInfo.pageNumber || 1;
		currentPageSize = resultInfo.pageSize || 10;
		console.log(currentPageCount + "," + currentPageNumber + "," + currentPageSize);
		//刷新分页数据
        refreshPagination();
    }
    
    function refreshPagination() {
        layui.use(['laypage', 'layer'], function() {
            var laypage = layui.laypage;
            //分页
            laypage.render({
                elem: 'demo7'
                ,count: currentPageCount
                ,curr: currentPageNumber
                ,limit: currentPageSize
                ,theme: '#34C0E3'
                ,layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
                ,jump: function(obj, first) {
                    if (!first) {
                        currentPageNumber = obj.curr;
                        currentPageSize = obj.limit;
                        var scope = getAngularScope();
                        scope.screen(currentPageNumber);
                        console.log(currentPageSize + ":" + currentPageNumber);
                    }
                }
            });
        });
    }

	//Demo
	layui.use(['form', 'laypage', 'laydate', 'table', 'layer'], function() {
		var form = layui.form;
		var laydate = layui.laydate;
		var laypage = layui.laypage

		//监听提交
		form.on('submit(formDemo)', function(data) {
			layer.msg(JSON.stringify(data.field));
			return false;
		});
        laydate.render({
            elem: '#test1' //指定元素
            ,theme: '#34C0E3'
        });
        laydate.render({
            elem: '#test2' //指定元素
            ,theme: '#34C0E3'
        });
		//分页
//		laypage.render({
//			elem: 'demo7',
//			count: 100,
//            theme: '#34C0E3',
//			layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
//			jump: function(obj) {
//				// console.log(obj)
//			}
//		});
		//创建单项
		$(function() {
			$('.creatdx').on('click', function() {
				var strU = 'add_paper.html';
				layer.open({
					type: 2,
					title: '创建问卷类型',
					area: ['480px', '500'],
					shadeClose: true, //开启遮罩关闭
					content: strU
				});
			});
            form.render('select', 'test_form');
		});
	});
    
//    function turnModify(paperId) {
//		console.log(paperId);
//		var url = "modifyPaper.html?paperId=" + paperId;
//		window.open(url,"");
//    }
</script>